//
//  HPSignView.m
//  HandpayDraw
//
//  Created by panbin on 13-11-21.
//  Copyright (c) 2013年 Handpay. All rights reserved.
//

#import "HPSignView.h"
#import "ZZTongDefine.h"

@interface HPSignView()

/**
 * 画笔的颜色
 */
@property (nonatomic, assign) CGColorRef drawColor;

/**
 * 画笔的宽度
 */
@property (nonatomic, assign) float drawWidth;

/**
 * 保持已经画过的点
 */
@property (nonatomic, retain) NSMutableArray *drawAllPoint;

/**
 * 保持已经画过的线
 */
@property (nonatomic, retain) NSMutableArray *drawAllLine;

/**
 * 画点开始位置
 */
@property (nonatomic, assign) CGPoint drawBeginPoint;

/**
 * 画点移动位置
 */
@property (nonatomic, assign) CGPoint drawMovePoint;



@end

@implementation HPSignView

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        _drawColor = [[UIColor blackColor] CGColor];
        //画笔大小
        _drawWidth = 3;
        
        [self initDrawPointArray];
        
        NSMutableArray* array = [[NSMutableArray alloc] init];
        self.drawAllLine = array;
        [array release];
    }
    
    return self;
}

//重写draw方法
- (void)drawRect:(CGRect)rect {
	//获取上下文
	CGContextRef context = UIGraphicsGetCurrentContext();
    
	//设置笔冒
	CGContextSetLineCap(context, kCGLineCapRound);
    
	//设置画线的连接处　拐点圆滑
	CGContextSetLineJoin(context, kCGLineJoinRound);
   
	//画之前线
	if ([_drawAllLine count] > 0) {
		for (int i = 0; i < [_drawAllLine count]; i++) {
            
			NSArray *tempArray = [NSArray arrayWithArray:[_drawAllLine objectAtIndex:i]];
            
			if ([tempArray count] > 1) {
                
				CGContextBeginPath(context);
                
				CGPoint myStartPoint = [[tempArray objectAtIndex:0] CGPointValue];
                
				CGContextMoveToPoint(context, myStartPoint.x, myStartPoint.y);
				
				for (int j = 0; j < [tempArray count] - 1; j++) {
                    
					CGPoint myEndPoint=[[tempArray objectAtIndex:j+1] CGPointValue];
					CGContextAddLineToPoint(context, myEndPoint.x, myEndPoint.y);
				}
                
//				CGContextSetStrokeColorWithColor(context, _drawColor);

				CGContextSetLineWidth(context, _drawWidth);
                
				CGContextStrokePath(context);
			}
		}
	}
    
	//画当前的线
	if ([_drawAllPoint count] > 1) {
		CGContextBeginPath(context);
		//起点
		CGPoint myStartPoint = [[_drawAllPoint objectAtIndex:0] CGPointValue];
		CGContextMoveToPoint(context, myStartPoint.x, myStartPoint.y);
        
		//把move的点全部加入数组
		for (int i = 0; i < [_drawAllPoint count] - 1; i++) {
			CGPoint myEndPoint =  [[_drawAllPoint objectAtIndex:i + 1] CGPointValue];
			CGContextAddLineToPoint(context, myEndPoint.x, myEndPoint.y);
		}
        
		CGContextSetStrokeColorWithColor(context, _drawColor);
		CGContextSetFillColorWithColor (context,  _drawColor);
        
		//绘制画笔宽度
		CGContextSetLineWidth(context, _drawWidth);
        
		//把数组里面的点全部画出来
		CGContextStrokePath(context);
	}
}

/**
 * 初始化存放点的数组
 */
- (void)initDrawPointArray {
    if (_drawAllPoint) {
        HP_RELEASE(_drawAllPoint);
    }
    
    _drawAllPoint = [[NSMutableArray alloc] init];
}

/**
 * 把画过的当前线放入　存放线的数组
 */
-(void)doAddPointToLine {
	[_drawAllLine addObject:_drawAllPoint];
}

/**
 * 保持画过的点
 */
-(void)doSavePoint:(CGPoint)point {
	NSValue *pointValue = [NSValue valueWithCGPoint:point];
	[_drawAllPoint addObject:pointValue];
}

/**
 * 撤销上一步操作
 */
-(void)doCancelLastStep {
	if ([_drawAllLine count] > 0) {
		[_drawAllLine  removeLastObject];
		[_drawAllPoint removeAllObjects];
	}
    
	[self setNeedsDisplay];
}

/**
 * 清除屏幕
 */
-(void)doClearScreen {
	if ([_drawAllLine count] > 0) {
		[_drawAllLine removeAllObjects];
		[_drawAllPoint removeAllObjects];
	}
    
    [self setNeedsDisplay];
}

/**
 * 是否在屏幕上已经画了线
 */
- (BOOL)isDrawedInScreen {
    BOOL isDrawed = NO;
    
    if ([_drawAllLine count] > 0) {
        isDrawed = YES;
    }
    
    return isDrawed;
}

#pragma mark -
#pragma mark TouchDelegate

//手指开始触屏开始
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    
	UITouch* touch = [touches anyObject];
    
    _drawBeginPoint = [touch locationInView:self];
    
    [self initDrawPointArray];
    
    [self doSavePoint:_drawBeginPoint];
}

//手指移动时候发出
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
	NSArray *movePointArray = [touches allObjects];
    
	_drawMovePoint = [[movePointArray objectAtIndex:0] locationInView:self];
    
    [self doSavePoint:_drawMovePoint];
    
	[self setNeedsDisplay];
    [NOTIFICATION postNotificationName:HPStartDrawNotificationKey object:[NSNumber numberWithBool:YES]];
}

//当手指离开屏幕时候
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [self doAddPointToLine];
    
	[self setNeedsDisplay];
}

- (void)dealloc {
    HP_RELEASE(_drawAllPoint);
    HP_RELEASE(_drawAllLine);
    [super dealloc];
}

@end
